﻿namespace SwyishChess.Domain.Board
{
    public class ChessBoardModel
    {
        // Column count
        private const int CHESS_BOARD_COLUMN = 8;
        // Row count
        private const int CHESS_BOARD_ROW = 8;

        /// <summary>
        /// Represents a chess board
        /// </summary>
        public ChessBlock[,] ChessBoard { get; set; }

        /// <summary>
        /// Default constructor to initializes the chessboard made of chess blocks and call initalizaion method
        /// </summary>
        public ChessBoardModel()
        {
            ChessBoard = new ChessBlock[CHESS_BOARD_COLUMN, CHESS_BOARD_ROW];
            InitializeChessBoard();
        }

        /// <summary>
        /// Initializes the chess board by instantiating each chess blocks in the board,
        /// and set a flag for color highlighting
        /// </summary>
        private void InitializeChessBoard()
        {
            for (int row = 0; row < CHESS_BOARD_ROW; row++)
            {
                for (int column = 0; column < CHESS_BOARD_COLUMN; column++)
                {
                    // Instantiate a new chess blocks
                    ChessBoard[row, column] = new ChessBlock();

                    // Set highlight flag as "on" if the row is 0 or even numbers and column is also 0 or even numbers
                    if (row == 0 || row % 2 == 0)
                    {
                        if (column == 0 || column % 2 == 0)
                        {
                            ChessBoard[row, column].Color = "off";
                        }
                        else
                        {
                            ChessBoard[row, column].Color = "on";
                        }
                    }
                    // Set highlight flag as "on" if the row is 1 or odd numbers and column is also 1 or odd numbers
                    else if (row == 1 || (row % 2 != 0 && row != 0))
                    {
                        if (column == 1 || (column % 2 != 0 && column != 0))
                        {
                            ChessBoard[row, column].Color = "off";
                        }
                        else
                        {
                            ChessBoard[row, column].Color = "on";
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Sets IsAvailalbeForMove property in the chess blocks in the board to false,
        /// so that it can remove all hightlights created to indicate possible moves
        /// </summary>
        public void RemoveMovableRange()
        {
            for (int row = 0; row < CHESS_BOARD_ROW; row++)
            {
                for (int column = 0; column < CHESS_BOARD_COLUMN; column++)
                {
                    ChessBoard[row, column].IsAvailableForMove = false;
                }
            }
        }

        public ChessBlock[,] CreateCopiedChessBoard()
        {
            ChessBlock[,] newBoard = new ChessBlock[8, 8];

            for (int row = 0; row < 8; row++)
            {
                for (int column = 0; column < 8; column++)
                {
                    newBoard[row, column] = new ChessBlock();
                    newBoard[row, column].IsAvailableForMove = ChessBoard[row, column].IsAvailableForMove;
                }
            }

            return newBoard;
        }
    }
}
